package com.yamanyar.weebill.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.yamanyar.weebill.dao.UserDAO;
import com.yamanyar.weebill.domain.User;
import com.yamanyar.weebill.service.UserService;

/**
 * Implementation of <code>UserService</code> interface. Created in Jun 28,
 * 2008.
 * 
 * @see UserService
 * @author Kaan Yamanyar
 */
@Service
public class UserServiceImpl implements UserService {

	private UserDAO userDAO;

	@Autowired
	public UserServiceImpl(UserDAO userDAO) {
		this.userDAO = userDAO;
	}

	public void createNew(User user) {
		userDAO.persist(user);
	}

	public List<User> findAll() {
		return userDAO.loadAll();
	}

	public List<User> findByFirstName(String firstName) {
		return userDAO.findByFirstName(firstName);
	}

	public User findByMail(String mail) {
		List<User> result = userDAO.findByMail(mail);
		int sizeOfResult = result.size();
		if (sizeOfResult == 0)
			return null;
		// else if (sizeOfResult > 1) {
		// throw new IllegalBusinessState(
		// "There are more then one User's with the given name.");
		//		}
		return result.get(0);
	}

	public User getUserWithID(Long ID) {
		return userDAO.loadById(ID);
	}
}
